package com.ruoyi.business.controller;

import com.ruoyi.business.service.ClassPermissionsService;
import com.ruoyi.business.service.IWsdClassesService;
import com.ruoyi.business.service.IWsdStudentsService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.WsdClasses;
import com.ruoyi.common.core.domain.entity.WsdStudents;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 班级信息Controller
 *
 * @author ruoyi
 * @date 2024-04-14
 */
@RestController
@RequestMapping("wsd_classes")
public class WsdClassesController extends BaseController
{
    @Autowired
    private IWsdClassesService wsdClassesService;
    @Autowired
    private ClassPermissionsService classPermissionsService;
    @Autowired
    private IWsdStudentsService wsdStudentsService;
    /**
     * 查询班级信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:classes:list')")
    @GetMapping("/list")
    public TableDataInfo list(WsdClasses wsdClasses)
    {
        SysUser user = getLoginUser().getUser();
        Long schoolId = user.getSchoolId();
        //权限
        if (!user.isAdmin()) {//过滤掉admin账号
            if (schoolId == null) {
                List<WsdClasses> emptyList = new ArrayList<>();
                return getDataTable( emptyList );
            }
            if (!"business".equals( user.getRoles().get( 0 ).getRoleKey() )) {//过滤调学校管理员角色
                String classIds = classPermissionsService.classIds();
                wsdClasses.setClassIds( classIds );
            }
            wsdClasses.setSchoolId( schoolId );
        }
        startPage();
        List<WsdClasses> list = wsdClassesService.selectWsdClassesList(wsdClasses);
        return getDataTable(list);
    }

    /**
     * 导出班级信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:classes:export')")
    @Log(title = "班级信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WsdClasses wsdClasses)
    {
        List<WsdClasses> list = wsdClassesService.selectWsdClassesList(wsdClasses);
        ExcelUtil<WsdClasses> util = new ExcelUtil<WsdClasses>(WsdClasses.class);
        util.exportExcel(response, list, "班级信息数据");
    }

    /**
     * 获取班级信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:classes:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wsdClassesService.selectWsdClassesById(id));
    }

    /**
     * 新增班级信息
     */
    @PreAuthorize("@ss.hasPermi('business:classes:add')")
    @Log(title = "班级信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WsdClasses wsdClasses)
    {
        SysUser user = getLoginUser().getUser();
        Long schoolId = user.getSchoolId();
        wsdClasses.setSchoolId(schoolId);
        //检查别名有没有重复
        if (wsdClassesService.selectWsdClassesByName(wsdClasses.getName(),schoolId)!=null){
            return AjaxResult.warn("班级别名重复，请修改后重试");
        }
        return toAjax(wsdClassesService.insertWsdClasses(wsdClasses));
    }

    /**
     * 修改班级信息
     */
    @PreAuthorize("@ss.hasPermi('business:classes:edit')")
    @Log(title = "班级信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WsdClasses wsdClasses)
    {
        return toAjax(wsdClassesService.updateWsdClasses(wsdClasses));
    }

    /**
     * 删除班级信息
     */
    @PreAuthorize("@ss.hasPermi('business:classes:remove')")
    @Log(title = "班级信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for (Long id : ids){
            WsdStudents wsdStudents = new WsdStudents();
            wsdStudents.setClassId( id );
            List<WsdStudents> list = wsdStudentsService.selectWsdStudentsList(wsdStudents);
            if (list.size() > 0){
                return warn("该班级下有学生，不能删除!!请删除该班级学生后再重试");
            }
        }
        return toAjax(wsdClassesService.deleteWsdClassesByIds(ids));
    }
}
